package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.ConnectionFabric;
import model.Produtos;

/**
 * Servlet implementation class Servletbuscarprod
 */
@WebServlet("/ServletBuscarprod")
public class ServletBuscarprod extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletBuscarprod() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	
	{
		// TODO Auto-generated method stub
		String nome = request.getParameter("nome");
		String sql = "";
			
                    
		         
                   
				    sql = "select * from produtos WHERE nome like ?"; 
				    
				    try{
				    	
					    Connection con = ConnectionFabric.getConnection();	    	
					    PreparedStatement ps = con.prepareStatement(sql);
					    ps.setString(1,"%"+nome+"%"); 
					    ResultSet rs = ps.executeQuery();	
					    List<Produtos> lista = new ArrayList<Produtos>(); 
					    
					    while(rs.next()){
					    Produtos prod = new Produtos();
					    prod.setCod_produto(rs.getInt("cod_produto"));
					    prod.setNome(rs.getString("nome"));
					    prod.setDescricao(rs.getString("descricao"));
					    prod.setQuantidade(rs.getInt("quantidade"));
					    prod.setQuantidademin(rs.getInt("quantidademin"));
					    prod.setValorcusto(rs.getDouble("custo"));
					    prod.setValorvenda(rs.getDouble("valor"));
					    prod.setFornecedor(rs.getString("fornecedor"));
					    
					    lista.add(prod);    	
					    }
					    HttpSession sessao = request.getSession();
					    sessao.setAttribute("list", lista);  
					    RequestDispatcher rd = request.getRequestDispatcher("ProdConsulta.jsp");  
					    rd.forward(request, response); 
					    }catch(Exception ex){	    	
					    	ex.printStackTrace();	
					                        }	
				    
				    
		                                          } 
				
		                        	}